﻿<UserControl x:Class="VSTalk.Engine.View.ContactList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             xmlns:conerters="clr-namespace:VSTalk.Engine.Converters"
             xmlns:jas="clr-namespace:VSTalk.Engine.Utils"
             xmlns:contactList="clr-namespace:VSTalk.Engine.ViewModel.ContactList"
             xmlns:clientResources="clr-namespace:VSTalk.Engine.ViewModel.ContactList.ClientResources"
             xmlns:controls="clr-namespace:VSTalk.Engine.Controls"
             d:DesignHeight="300"
             d:DesignWidth="300"
             x:Name="ContactListView">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TreeView.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <conerters:ClientStateToColorConverter x:Key="ClientStateToColorConverter" />
            <conerters:VisibilityIfCanExecuteConverter x:Key="VisibilityIfCanExecuteConverter" />
            <conerters:VisibleIfDisconnected x:Key="VisibleIfDisconnected" />
            <conerters:VisibleIfConnected x:Key="VisibleIfConnected" />
            <conerters:VisibleIfFalseConverter x:Key="VisibleIfFalseConverter" />
            <conerters:VisibleIfTrueConverter x:Key="VisibleIfTrueConverter" />

            <HierarchicalDataTemplate DataType="{x:Type contactList:ClientNodeViewModel}"
                                    ItemsSource="{Binding ActiveResources}">
                <StackPanel Orientation="Vertical">
                    <Grid>
                        <Grid.Resources>
                            <jas:ElementSpy x:Key="ClientSpy" />
                        </Grid.Resources>
                        <Grid.ContextMenu>
                            <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.DataContext}">
                                <MenuItem Header="connect"
                        Visibility="{Binding Path=Element.DataContext.Context.State, Source={StaticResource ClientSpy}, Converter={StaticResource VisibleIfDisconnected}}"
                        Command="{Binding Source={StaticResource ClientSpy}, Path=Element.DataContext.ConnectClient}" />
                                <MenuItem Header="disconnect"
                        Visibility="{Binding Path=Element.DataContext.Context.State, Source={StaticResource ClientSpy}, Converter={StaticResource VisibleIfConnected}}"
                        Command="{Binding Source={StaticResource ClientSpy}, Path=Element.DataContext.DisconnectClient}" />
                                <MenuItem Header="add contact"
                        Command="{Binding Source={StaticResource ClientSpy}, Path=Element.DataContext.ShowContactEditor}" />
                                <MenuItem Header="add room"
                        Command="{Binding Source={StaticResource ClientSpy}, Path=Element.DataContext.ShowRoomEditor}" />
                            </ContextMenu>
                        </Grid.ContextMenu>
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="15"
                       Height="10"
                       Margin="5"
                       Fill="{Binding Context.State, Converter={StaticResource ClientStateToColorConverter}}" />
                            <TextBlock Text="{Binding Model.Login}"
                       Margin="5" />
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type clientResources:NullClientResource}">
                <Grid>
                    <TextBlock Margin="5"
                   Text="{Binding Name}" />
                </Grid>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type clientResources:RoomResourceViewModel}">
                <Grid>
                    <Grid.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"
                        Command="{Binding OpenRoom}" />
                    </Grid.InputBindings>
                    <Grid.ContextMenu>
                        <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.DataContext}">
                            <MenuItem Header="join"
                      Command="{Binding JoinRoom}"
                      Visibility="{Binding Context.IsActive, Converter={StaticResource VisibleIfFalseConverter}}" />
                            <MenuItem Header="leave"
                      Command="{Binding LeaveRoom}"
                      Visibility="{Binding Context.IsActive, Converter={StaticResource VisibleIfTrueConverter}}" />
                            <MenuItem Header="delete"
                      Command="{Binding DeleteRoom}" />
                            <MenuItem Header="log"
                      Command="{Binding ShowLog}" />
                        </ContextMenu>
                    </Grid.ContextMenu>
                    <controls:RoomLabel Room="{Binding Room}" />
                </Grid>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type clientResources:InterlocutorResourceViewModel}">
                <Grid>
                    <Grid.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"
                        Command="{Binding ShowInterlocutorChat}" />
                    </Grid.InputBindings>
                    <Grid.ContextMenu>
                        <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.DataContext}">
                            <MenuItem Header="request authorization"
                      Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource VisibilityIfCanExecuteConverter}}"
                      Command="{Binding SendRequestAuthorization}" />
                            <MenuItem Header="remove"
                      Command="{Binding RemoveContact}" />
                            <MenuItem Header="log"
                      Command="{Binding ShowContactLog}" />

                        </ContextMenu>
                    </Grid.ContextMenu>
                    <controls:InterlocutorLabel Interlocutor="{Binding Model}">
                        <controls:InterlocutorLabel.ToolTip>
                            <controls:InterlocutorToolTip Summary="{Binding Summary}" />
                        </controls:InterlocutorLabel.ToolTip>
                    </controls:InterlocutorLabel>
                </Grid>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type contactList:TreeContactLayout}">
                <TreeView ItemsSource="{Binding Clients}"
              HorizontalContentAlignment="Stretch">
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MetroTreeViewItem}">
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </TreeView.ItemContainerStyle>
                </TreeView>
            </DataTemplate>
            <DataTemplate DataType="{x:Type contactList:PlotContactLayout}">
                <ListView ItemsSource="{Binding ActiveResources, Mode=OneWay}"
              HorizontalContentAlignment="Stretch">
                </ListView>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ContentPresenter Content="{Binding PlotLayout, Mode=OneWay}" 
                      Visibility="{Binding IsActive, Converter={StaticResource VisibleIfTrueConverter}}"/>
        <ContentPresenter Content="{Binding TreeLayout, Mode=OneWay}" 
                      Visibility="{Binding IsActive, Converter={StaticResource VisibleIfTrueConverter}}"/>
    </Grid>
</UserControl>
